using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace App
{

	public partial class MdiTabStripButton : ToolStripSplitButton
	{

		private Form m_MdiChild;
        private System.ComponentModel.IContainer components = null;


		public MdiTabStripButton(Form mdiChild)
		{
			if (null == mdiChild)
				throw new ArgumentNullException("mdiChild");
            
			InitializeComponent();
			SetMdiChild(mdiChild);

		}

		private void SetMdiChild(Form f)
		{
			if (null == f)
				return;

			if (null != m_MdiChild)
			{
				m_MdiChild.Activated -= new EventHandler(FormActivated);
				m_MdiChild.HandleDestroyed -= new EventHandler(FormHandleDestroyed);
				m_MdiChild.TextChanged -= new EventHandler(FormTextChanged);
			}
			m_MdiChild = f;
			Text = "         " + f.Text + "      ";
			f.Activated += new EventHandler(FormActivated);
			f.HandleDestroyed += new EventHandler(FormHandleDestroyed);
			f.TextChanged += new EventHandler(FormTextChanged);
		}

		private void FormActivated(object sender, EventArgs e)
		{
			MdiTabStrip ts = Owner as MdiTabStrip;
			if (null != ts)
				ts.SelectedTab = this;

		}


		private void FormTextChanged(object sender, EventArgs e)
		{
			Form f = sender as Form;
			Text = f.Text;
		}

		private void FormHandleDestroyed(object sender, EventArgs e)
		{
			Form f = sender as Form;
			f.Activated -= new EventHandler(FormActivated);
			f.TextChanged -= new EventHandler(FormTextChanged);
			f.HandleDestroyed -= new EventHandler(FormHandleDestroyed);
			Owner.Items.Remove(this);
		}


		protected override Padding DefaultMargin
		{
			get { return new Padding(4, 2, 4, 0); }
		}


		protected override void OnPaint(PaintEventArgs e)
		{
            base.OnPaint(e);
			Graphics g = e.Graphics;

            g.SmoothingMode = SmoothingMode.AntiAlias;

            BackColor = IsSelectedTab ? Color.FromArgb(226, 236, 248) : Color.FromArgb(191, 219, 255);
            using (Brush b = new SolidBrush(BackColor))
            {
                g.FillRectangle(b, e.ClipRectangle);
            }

            Rectangle tabrect = e.ClipRectangle;
            tabrect.Height += 4;
            tabrect.Width -= 1;

            if (IsSelectedTab)
            {
                using (SolidBrush b = new SolidBrush(Color.FromArgb(32, Color.Black)))
                {
                    DrawHelper.FillTopRoundRectangle(e.Graphics, b,
                        new Rectangle(tabrect.X - 2, tabrect.Y, tabrect.Width + 4, tabrect.Height + 4), 3f);
                }

                using (SolidBrush b = new SolidBrush(Color.FromArgb(16, Color.Black)))
                {
                    DrawHelper.FillTopRoundRectangle(e.Graphics, b,
                        new Rectangle(tabrect.X - 1, tabrect.Y, tabrect.Width + 2, tabrect.Height + 4), 3f);
                }

                using (LinearGradientBrush b = new LinearGradientBrush(tabrect, Color.FromArgb(235, 243, 254), Color.FromArgb(220, 231, 245), LinearGradientMode.Vertical))
                {
                    DrawHelper.FillTopRoundRectangle(e.Graphics, b, tabrect, 3f);
                }

                DrawHelper.DrawTopRoundRectangle(e.Graphics, new Pen(Color.FromArgb(141, 178, 227)), tabrect, 3f);
                tabrect.Offset(1, 1);
                tabrect.Width -= 2;
                tabrect.Height--;

                using (LinearGradientBrush b = new LinearGradientBrush(tabrect, Color.FromArgb(248, 251, 255),
                    Color.Transparent, LinearGradientMode.ForwardDiagonal))
                {
                    DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(b), tabrect, 3f);
                }

                //DrawClose
                Rectangle blank = DropDownButtonBounds;
                int rxx = blank.X - 4;
                int rsize = blank.Width - 4;
                Rectangle r = new Rectangle(rxx, rsize + 6 , rsize, rsize);
                using (Pen p = new Pen(Color.DarkOrange, 2.0F))
                {
                    g.DrawLine(p, r.Left, r.Top, r.Left + r.Width, r.Top + r.Height);
                    g.DrawLine(p, r.Left, r.Top + r.Height, r.Left + r.Width, r.Top);
                }
            }
	
            //if (DrawBorder)
            //    DoDrawBorder(g);

            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Center;

            g.DrawString(Text, Font, new SolidBrush(Color.FromArgb(21, 66, 139)), e.ClipRectangle, sf);
		}


		protected override void OnButtonClick(EventArgs e)
		{
			base.OnButtonClick(e);
			Form f = GetMdiChild();
			if (null != f)
				f.Activate();
		}

		protected override void OnDropDownShow(EventArgs e)
		{
			base.OnDropDownShow(e);
            HideDropDown();
            Form f = GetMdiChild();
            if (null != f && IsSelectedTab)
                f.Close();
		}


        public override Color BackColor
        {
            get
            {
                return Color.FromArgb(191, 219, 255);
                //return BackColor = IsSelectedTab ? Color.FromArgb(226, 236, 248) : Color.FromArgb(191, 219, 255);
            }
        }

		[Browsable(false)]
		public bool IsSelectedTab
		{
			get
			{
				MdiTabStrip ts = Owner as MdiTabStrip;
				if (null == ts || !Equals(ts.SelectedTab))
					return false;

				return true;
			}
		}

		public Form GetMdiChild()
		{
			return m_MdiChild;
		}

        protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

        private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
	}
}
